﻿using ShanghaiMetroAPP.MetroDraw;
using ShanghaiMetroAPP.Model;
using ShanghaiMetroAPP.Model.SMAPPControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace ShanghaiMetroAPP.ViewModel
{
    public class MainWindowViewModel : MainWindowModel
    {
        public MainWindowViewModel() : base()
        {
            on_search_text_changed_event_handle += this.onSearchTextChanged;
        }
        private event Action<string> on_search_text_changed_event_handle;
        /// <summary>
        /// 以StationCode为参数，更新线路信息。
        /// </summary>
        /// <param name="stationCode"></param>
        public void UpdateLineInfoList(string stationCode)
        {
            this.DirectionList = DirectionInfoListModel.HttpGet(stationCode);
        }


        public string SearchText
        {
            get => _search_text;
            set
            {
                if (_search_text == value)
                    return;
                _search_text = value;
                IsSearchStationOpen = true;
                notify(nameof(SearchText));
                on_search_text_changed_event_handle?.Invoke(value);
            }
        }
        /// <summary>
        /// 异步方法，筛选所有数据
        /// </summary>
        /// <param name="changedText"></param>
        protected async void onSearchTextChanged(string changedText)
        {
            var cvs = CollectionViewSource.GetDefaultView(this.Stations);
            if (cvs != null && cvs.CanFilter)
            {
                if (string.IsNullOrEmpty(changedText))
                {
                    cvs.Filter = (v) => { return true; };
                }
                else
                {
                    cvs.Filter = (v) =>
                    {
                        var station = v as Station;
                        if (station != null)
                        {
                            return
                                station.Name.Contains(changedText) ||
                                station.EnName.ToUpper().Contains(changedText) ||
                                station.PinyinName.ToUpper().Contains(changedText.ToUpper());
                        }
                        else
                        {
                            return true;
                        }
                    };
                }
            }

        }

        public int SearchIndex
        {
            get => _search_index;
            set
            {
                _search_index = value;
                notify(nameof(SearchIndex));
            }
        }

        private bool _is_search_station_open = false;
        private string _search_text;
        private int _search_index = -1;
        private Station _select_station;

        public Station SelectStation
        {
            get => _select_station;
            set
            {
                _select_station = value;
                notify(nameof(SelectStation));
            }
        }

        public bool IsSearchStationOpen
        {
            get => _is_search_station_open;
            set
            {
                _is_search_station_open = value;
                notify(nameof(IsSearchStationOpen));
            }
        }

    }
}
